/* $RCSFile: PagesDB.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/ADNCRM.Core/DAL/PagesDB.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:28  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Threading;
using System.Diagnostics;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Xml;
using System.Globalization;

using AddonNice.Helpers;
using AddonNice.Settings;
using AddonNice.Configuration.Settings;
using AddonNice.Diagnostics;

namespace AddonNice.Configuration
{

	/// <summary>
	/// Class that encapsulates all data logic necessary to add/query/delete
	/// Portals within the Portal database.
	/// </summary>
    public class PagesDB : DBDataStore
	{
		#region Declarations
		
        string DBAllUser2			=	Config.InternallAllUsersRole+";";

        // New const for new method AddPage defaults
		const int intParentPageID	= 0;  
		#endregion

        public PagesDB()
        {}

        #region Add Page

		/// <summary>
		/// The AddPage method adds a new tab to the portal. Page Visible for All Users
		/// AddPage Stored Procedure
		/// </summary>
		public int AddPage(int portalID, string pageName,string pageImage, int pageOrder,
            int NbPanes,int width0, int width1, int width2, int width3, int width4) 
		{
			return AddPage(portalID,intParentPageID,pageName,pageImage, pageOrder,NbPanes,width0,width1,width2,width3,width4, DBAllUser2);
		}

        /*
		/// <summary>
		/// The AddPage method adds a new tab to the portal.
		/// AddPage Stored Procedure
		/// </summary>
		public int AddPage(int portalID, string pageName,string pageImage, string Roles, int pageOrder,
            int NbPanes,int width0, int width1, int width2, int width3, int width4) 
		{
			// Change Method to use new all parms method below
			// SP call moved to new method AddPage below.
			return AddPage(portalID, intParentPageID, pageName,pageImage, pageOrder,NbPanes,width0,width1,width2,width3,width4,Roles);
		}
        */

		/// <summary>
		/// The AddPage method adds a new tab to the portal.
		/// AddPage Stored Procedure
		/// </summary>
        public int AddPage(int portalID, int parentPageID, string pageName,string pageImage, int pageOrder, 
            int NbPanes,int width0, int width1, int width2, int width3, int width4, string authorizedRoles)
        {
            SqlConnection theConnection         =   myConnection;
            if ( theConnection.State == ConnectionState.Open )
                theConnection		            =   newConnection;
            using (SqlCommand myCommand         =   new SqlCommand("dbo.adn_AddTab", theConnection))
            {
                myCommand.CommandType			=   CommandType.StoredProcedure;

                AddPortalIDParameters(myCommand,portalID);

                SqlParameter parameterParentPageID = new SqlParameter(StStr.ATParentPageID, SqlDbType.Int, 4);
                parameterParentPageID.Value		=   parentPageID;
                myCommand.Parameters.Add(parameterParentPageID);

                //Fixes a missing tab name
                if (string.IsNullOrEmpty(pageName) ) 
					pageName					=   "New Tab";
                SqlParameter parameterTabName	=   new SqlParameter(StStr.ATPageName, SqlDbType.NVarChar, 50);
                //Fixes tab name to long
                if (pageName.Length > 50) 
						parameterTabName.Value	=   pageName.Substring(0, 49);
                else	parameterTabName.Value	=   pageName;
                myCommand.Parameters.Add(parameterTabName);

                SqlParameter parameterTabImage	=   new SqlParameter(StStr.ATPageSkin, SqlDbType.NVarChar, 50);
                parameterTabImage.Value	        =   pageImage.Trim();
                myCommand.Parameters.Add(parameterTabImage);

                SqlParameter parameterTabOrder	=   new SqlParameter(StStr.ATPageOrder, SqlDbType.Int, 4);
                parameterTabOrder.Value			=   pageOrder;
                myCommand.Parameters.Add(parameterTabOrder);

                SqlParameter parameterNbPanes   =   new SqlParameter(StStr.ATNbPanes, SqlDbType.Int, 4);
                parameterNbPanes.Value          =   NbPanes;
			    myCommand.Parameters.Add(parameterNbPanes);

                SqlParameter parameterWidth0    =   new SqlParameter(StStr.ATWidth0, SqlDbType.Int, 4);
                parameterWidth0.Value           =   width0;
			    myCommand.Parameters.Add(parameterWidth0);

                SqlParameter parameterWidth1    =   new SqlParameter(StStr.ATWidth1, SqlDbType.Int, 4);
                parameterWidth1.Value           =   width1;
			    myCommand.Parameters.Add(parameterWidth1);

                SqlParameter parameterWidth2   =    new SqlParameter(StStr.ATWidth2, SqlDbType.Int, 4);
                parameterWidth2.Value          =    width2;
			    myCommand.Parameters.Add(parameterWidth2);

                SqlParameter parameterWidth3    =   new SqlParameter(StStr.ATWidth3, SqlDbType.Int, 4);
                parameterWidth3.Value           =   width3;
			    myCommand.Parameters.Add(parameterWidth3);

                SqlParameter parameterWidth4    =   new SqlParameter(StStr.ATWidth4, SqlDbType.Int, 4);
                parameterWidth4.Value           =   width4;
			    myCommand.Parameters.Add(parameterWidth4);

                SqlParameter parameterAuthRoles =   new SqlParameter(StStr.ATAuthorizedRoles, SqlDbType.NVarChar, 512);
                parameterAuthRoles.Value		=   authorizedRoles;
                myCommand.Parameters.Add(parameterAuthRoles);
                //output
                SqlParameter parameterPageID	=	new SqlParameter(StStr.ATPageID, SqlDbType.Int, 4);
                parameterPageID.Direction		=   ParameterDirection.Output;
                myCommand.Parameters.Add(parameterPageID);

                int ret							=   -1;
                try
                {
                    theConnection.Open();
                    myCommand.ExecuteNonQuery();
                    ret                         =   (int)parameterPageID.Value;
                }
                catch (Exception exp)
                {
                    if ( DBTraceSwitch.Sw.TraceError )
                        Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]PagesDB AddTab portalID: {0},tabName: {1}, ex: {2}", portalID, pageName, exp),DBTraceSwitch.Sw.Info);
                    throw;
                }
                finally
                {
                    if ( theConnection.State == System.Data.ConnectionState.Open )
                        theConnection.Close();
                }
                return ret;
            }
        }


		#endregion

		#region Delete Page

        /// <summary>
		/// If current page is in pages recycler portal deletes the page
        /// else transfers page in pages recycler portal and empties its ParentTabID
		/// </summary>
        public void DeletePage(int PageID)
        {
            using (SqlCommand myCommand         = new SqlCommand("dbo.adn_DeleteTab", myConnection))
            {
                myCommand.CommandType			= CommandType.StoredProcedure;
                
                AddPageIDParameters(myCommand,PageID);

                try
                {
                    myConnection.Open();
                    myCommand.ExecuteNonQuery();
					// remove all cache occurences which reference pages
                    AddonNice.Settings.Cache.CurrentCache.RemoveGlobalPagesCacheKey();
                    AddonNice.Settings.Cache.CurrentCache.RemoveAll(StStr.PageNavigationSettings);
                    AddonNice.Settings.Cache.CurrentCache.RemoveAll(StStr.AuthorizedPageStripDetail);
                }
                catch (Exception exp)
                {
                    if ( DBTraceSwitch.Sw.TraceError )
                        Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]PagesDB DeleteTab pageID: {0}, ex: {1}", PageID, exp),DBTraceSwitch.Sw.Info);
                    throw;
                }
                finally
                {
                    if (myConnection.State == System.Data.ConnectionState.Open)
                        myConnection.Close();
                }
            }
        }

		#endregion

        #region Recover from recycler
        
        public void RecoverPage(int PageID,int newPortalID,int newParentPageID,int TabLayout)
        {
            SqlCommand myCommand                    =   new SqlCommand("dbo.adn_RecoverPage", myConnection);
            myCommand.CommandType	                =   CommandType.StoredProcedure;
                
            AddPageIDParameters(myCommand,PageID);

                
            SqlParameter parameterNewPortalID	    =   new SqlParameter("@NewPortalID", SqlDbType.Int, 4);
            parameterNewPortalID.Value			    =   newPortalID;
            myCommand.Parameters.Add(parameterNewPortalID);
            
            SqlParameter parameterNewParentPageID	=   new SqlParameter("@NewParentPageID", SqlDbType.Int, 4);
            parameterNewParentPageID.Value			=   newParentPageID;
            myCommand.Parameters.Add(parameterNewParentPageID);

            SqlParameter parameterTabLayout	        =   new SqlParameter("@TabLayout", SqlDbType.Int, 4);
            parameterTabLayout.Value			    =   TabLayout;
            myCommand.Parameters.Add(parameterTabLayout);
            
            try
            {
                myConnection.Open();
                myCommand.ExecuteNonQuery();

                // remove all cache occurences which reference pages
                AddonNice.Settings.Cache.CurrentCache.RemoveGlobalPagesCacheKey();
                AddonNice.Settings.Cache.CurrentCache.RemoveAll(StStr.PageNavigationSettings);
                AddonNice.Settings.Cache.CurrentCache.RemoveAll(StStr.AuthorizedPageStripDetail);
            }
            catch (Exception exp)
            {
                if ( DBTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]PagesDB RecoverPage pageID: {0}, ex: {1}", PageID, exp),DBTraceSwitch.Sw.Info);
                throw;
            }
            finally
            {
                if (myConnection.State == System.Data.ConnectionState.Open)
                    myConnection.Close();
            }
        }

        #endregion Recover from recycler

        #region Update Pages

        /// <summary>
		/// UpdatePage Method<br />
		/// UpdatePage Stored Procedure
		/// </summary>
        public void UpdatePage(int portalID, int pageID, int parentPageID, string pageName,string pageImage, int pageOrder, 
            int NbPanes,int width0, int width1, int width2, int width3, int width4, string authorizedRoles)
        {
            SqlConnection theConnection             =   myConnection;
            if ( theConnection.State == ConnectionState.Open )
                theConnection		                =   new SqlConnection(Config.ConnectionString);
            using (SqlCommand myCommand = new SqlCommand("dbo.adn_UpdateTab", theConnection))
            {
                myCommand.CommandType				= CommandType.StoredProcedure;
                
                AddPortalIDParameters(myCommand,portalID);
                
                AddPageIDParameters(myCommand,pageID);
                
                SqlParameter parameterParentPageID	= new SqlParameter("@ParentTabID", SqlDbType.Int, 4);
                parameterParentPageID.Value			= parentPageID;
                myCommand.Parameters.Add(parameterParentPageID);

                //Fixes a missing tab name
                if ( string.IsNullOrEmpty(pageName)) 
					pageName = "&nbsp;";
                SqlParameter parameterTabName		=   new SqlParameter("@TabName", SqlDbType.NVarChar, 50);
                if (pageName.Length > 50) 
					parameterTabName.Value          =   pageName.Substring(0, 49);
                else parameterTabName.Value			=   pageName;
                myCommand.Parameters.Add(parameterTabName);

                SqlParameter parameterTabImage	    =   new SqlParameter("@TabSkinID", SqlDbType.NVarChar, 50);
                parameterTabImage.Value	            =   pageImage.Trim();
                myCommand.Parameters.Add(parameterTabImage);
                
                SqlParameter parameterTabOrder		= new SqlParameter("@TabOrder", SqlDbType.Int, 4);
                parameterTabOrder.Value				= pageOrder;
                myCommand.Parameters.Add(parameterTabOrder);
                
                SqlParameter parameterNbPanes           = new SqlParameter(StStr.ATNbPanes, SqlDbType.Int, 4);
                parameterNbPanes.Value                  =   NbPanes;
			    myCommand.Parameters.Add(parameterNbPanes);

                SqlParameter parameterWidth0            = new SqlParameter(StStr.ATWidth0, SqlDbType.Int, 4);
                parameterWidth0.Value                  =   width0;
			    myCommand.Parameters.Add(parameterWidth0);

                SqlParameter parameterWidth1            = new SqlParameter(StStr.ATWidth1, SqlDbType.Int, 4);
                parameterWidth1.Value                  =   width1;
			    myCommand.Parameters.Add(parameterWidth1);

                SqlParameter parameterWidth2            = new SqlParameter(StStr.ATWidth2, SqlDbType.Int, 4);
                parameterWidth2.Value                  =   width2;
			    myCommand.Parameters.Add(parameterWidth2);

                SqlParameter parameterWidth3            = new SqlParameter(StStr.ATWidth3, SqlDbType.Int, 4);
                parameterWidth3.Value                  =   width3;
			    myCommand.Parameters.Add(parameterWidth3);

                SqlParameter parameterWidth4            = new SqlParameter(StStr.ATWidth4, SqlDbType.Int, 4);
                parameterWidth4.Value                  =   width4;
			    myCommand.Parameters.Add(parameterWidth4);

                SqlParameter parameterAuthRoles		= new SqlParameter("@AuthorizedRoles", SqlDbType.NVarChar, 512);
                parameterAuthRoles.Value			= authorizedRoles;
                myCommand.Parameters.Add(parameterAuthRoles);

                try
                {
                    theConnection.Open();
                    myCommand.ExecuteNonQuery();
                }
                catch (Exception exp)
                {
                    if ( DBTraceSwitch.Sw.TraceError )
                        Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]PagesDB UpdateTab portalID: {0},tabName: {1}, ex: {2}", portalID, pageName, exp),DBTraceSwitch.Sw.Info);
                    throw;
                }
                finally
                {
                    if (theConnection.State == ConnectionState.Open)
                        theConnection.Close();
                }
            }
        }


		/// <summary>
		/// The UpdatePageOrder method changes the position of the page with respect to other pages in the portal.<br />
		/// UpdatePageOrder Stored Procedure			
		/// </summary>
        public void UpdatePageOrder(int PageID, int pageOrder)
        {
            SqlConnection theConnection         =   myConnection;
            if ( theConnection.State == ConnectionState.Open )
                theConnection		            =   new SqlConnection(Config.ConnectionString);
            using (SqlCommand myCommand         = new SqlCommand("dbo.adn_UpdateTabOrder", theConnection))
            {
                myCommand.CommandType			= CommandType.StoredProcedure;
                
                AddPageIDParameters(myCommand,PageID);

                SqlParameter parameterTabOrder	= new SqlParameter("@TabOrder", SqlDbType.Int, 4);
                parameterTabOrder.Value			= pageOrder;
                myCommand.Parameters.Add(parameterTabOrder);

                try
                {
                    theConnection.Open();
                    myCommand.ExecuteNonQuery();
                }
                catch (Exception exp)
                {
                    if ( DBTraceSwitch.Sw.TraceError )
                        Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]PagesDB UpdateTabOrder pageID: {0}, pageOrder: {1}, ex: {2}", PageID, pageOrder, exp),DBTraceSwitch.Sw.Info);
                    throw;
                }
                finally
                {
                    if (theConnection.State == System.Data.ConnectionState.Open)
                        theConnection.Close();
                }
            }
        }

		/// <summary>
		/// The UpdatePageOrder method changes the position of the page with respect to other pages in the portal.<br />
		/// UpdatePageOrder Stored Procedure			
		/// </summary>
        public void UpdatePageName(int pageID, string pageName)
        {
            using (SqlCommand myCommand             = new SqlCommand("dbo.adn_UpdateTabName", myConnection))
            {
                myCommand.CommandType			    = CommandType.StoredProcedure;
                
                AddPageIDParameters(myCommand,pageID);
                
                SqlParameter parameterTabName		= new SqlParameter("@TabName", SqlDbType.NVarChar, 50);
                if (pageName.Length > 50) 
					parameterTabName.Value = pageName.Substring(0, 49);
                else parameterTabName.Value			= pageName;
                myCommand.Parameters.Add(parameterTabName);

                try
                {
                    myConnection.Open();
                    myCommand.ExecuteNonQuery();
                }
                catch (Exception exp)
                {
                    if ( DBTraceSwitch.Sw.TraceError )
                        Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]PagesDB UpdatePageName pageID: {0}, pageName: {1}, ex: {2}", pageID, pageName, exp),DBTraceSwitch.Sw.Info);
                    throw;
                }
                finally
                {
                    if (myConnection.State == System.Data.ConnectionState.Open)
                        myConnection.Close();
                }
            }
        }

		/// <summary>
		/// The UpdatePageParent method changes the parent of the Page
		/// adn_UpdateTabParent Stored Procedure
		/// </summary>
        public void UpdatePageParent(int pageID, int parentPageID, int portalID)
        {
            using (SqlCommand myCommand             = new SqlCommand("dbo.adn_UpdateTabParent", myConnection))
            {
                myCommand.CommandType				= CommandType.StoredProcedure;

                AddPortalIDParameters(myCommand,portalID);
                
                AddPageIDParameters(myCommand,pageID);

                SqlParameter parameterParentPageID	= new SqlParameter("@ParentTabID", SqlDbType.Int, 4);
                parameterParentPageID.Value			= parentPageID;
                myCommand.Parameters.Add(parameterParentPageID);

                try
                {
                    myConnection.Open();
                    myCommand.ExecuteNonQuery();
                }
                catch (Exception exp)
                {
                    if ( DBTraceSwitch.Sw.TraceError )
                        Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]PagesDB UpdatePageParent pageID: {0}, parentPageID: {1}, ex: {2}", pageID, parentPageID, exp),DBTraceSwitch.Sw.Info);
                    throw;
                }
                finally
                {
                    if (myConnection.State == System.Data.ConnectionState.Open)
                        myConnection.Close();
                }
            }
        }

        public void UpdatePagePanes(int pageID, int NbPanes,int width0, int width1, int width2, int width3, int width4)
        {
            using (SqlCommand myCommand = new SqlCommand("dbo.adn_UpdatePagePanes", myConnection))
            {
                myCommand.CommandType				    = CommandType.StoredProcedure;

                AddPageIDParameters(myCommand,pageID);
                
                SqlParameter parameterNbPanes           = new SqlParameter(StStr.ATNbPanes, SqlDbType.Int, 4);
                parameterNbPanes.Value                  =   NbPanes;
			    myCommand.Parameters.Add(parameterNbPanes);

                SqlParameter parameterWidth0            = new SqlParameter(StStr.ATWidth0, SqlDbType.Int, 4);
                parameterWidth0.Value                   =   width0;
			    myCommand.Parameters.Add(parameterWidth0);

                SqlParameter parameterWidth1            = new SqlParameter(StStr.ATWidth1, SqlDbType.Int, 4);
                parameterWidth1.Value                   =   width1;
			    myCommand.Parameters.Add(parameterWidth1);

                SqlParameter parameterWidth2            = new SqlParameter(StStr.ATWidth2, SqlDbType.Int, 4);
                parameterWidth2.Value                   =   width2;
			    myCommand.Parameters.Add(parameterWidth2);

                SqlParameter parameterWidth3            = new SqlParameter(StStr.ATWidth3, SqlDbType.Int, 4);
                parameterWidth3.Value                   =   width3;
			    myCommand.Parameters.Add(parameterWidth3);

                SqlParameter parameterWidth4            = new SqlParameter(StStr.ATWidth4, SqlDbType.Int, 4);
                parameterWidth4.Value                   =   width4;
			    myCommand.Parameters.Add(parameterWidth4);

                try
                {
                    myConnection.Open();
                    myCommand.ExecuteNonQuery();
                }
                catch (Exception exp)
                {
                    if ( DBTraceSwitch.Sw.TraceError )
                        Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]PagesDB UpdatePagePanes pageID: {0}, ex : {1}", pageID, exp),DBTraceSwitch.Sw.Info);
                    throw;
                }
                finally
                {
                    if (myConnection.State == System.Data.ConnectionState.Open)
                        myConnection.Close();
                }
            }
        }

        #endregion

		#region Get Pages/Tabs

		/// <summary>
		/// Get all the pages in this portal ordered by asc parentTabID and order.
        /// If current portal is admin returns all pages, else return only public pages from adn_sitemap
		/// </summary>
		public SqlDataReader GetAllPortalPages(int portalID)
		{
            SqlConnection theConnection             =   newConnection;
			SqlCommand myCommand				    =   
                new SqlCommand(( Config.IsAdminPortal ) ? "dbo.adn_GetAllPortalTabs": "dbo.adn_GetAllPortalPages", theConnection);
			myCommand.CommandType				    =   CommandType.StoredProcedure;
			
            AddPortalIDParameters(myCommand,portalID);

            SqlParameter parameterPortalLanguage	=   new SqlParameter(StStr.ATPortalLanguage, SqlDbType.NVarChar, 12);
            parameterPortalLanguage.Value			=   base.CurrentUICultureName;
            myCommand.Parameters.Add(parameterPortalLanguage);

            SqlDataReader result				    =   null;
            try
            {
                theConnection.Open();
                result							    =   myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch (Exception exp)
            {
                if ( DBTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]PagesDB GetAllPortalPages portalID: {0}, ex: {1}", portalID, exp),DBTraceSwitch.Sw.Info);
                if (result != null)
                    result.Close();
                if (theConnection.State == System.Data.ConnectionState.Open)
                    theConnection.Close();
                throw;
            }
            return result;
        }

        /// <summary>
		/// Read only the page with transmitted ID
		/// </summary>
		public SqlDataReader GetPortalPage(int pageID)
		{
            SqlConnection theConnection             =   newConnection;
			SqlCommand myCommand				    =   new SqlCommand("dbo.adn_GetPortalPage", theConnection);
			myCommand.CommandType				    =   CommandType.StoredProcedure;

            AddPageIDParameters(myCommand,pageID);

            SqlParameter parameterPortalLanguage	=   new SqlParameter(StStr.ATPortalLanguage, SqlDbType.NVarChar, 12);
            parameterPortalLanguage.Value			=   base.CurrentUICultureName;
            myCommand.Parameters.Add(parameterPortalLanguage);

            SqlDataReader result				    =   null;
            try
            {
                theConnection.Open();
                result							    =   myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch (Exception exp)
            {
                if ( DBTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]PagesDB GetPortalPage pageID: {0} ex: {1}",pageID, exp),DBTraceSwitch.Sw.Info);
                if (result != null)
                    result.Close();
                if (theConnection.State == System.Data.ConnectionState.Open)
                    theConnection.Close();
                throw;
            }
            return result;
        }

		/// <summary>
		/// Get all the pages in this portal ordered by asc parentTabID and order.
		/// Returns a DataTable
		/// </summary>
		public DataSet GetTbAllPortalPages(int portalID)
		{
			SqlCommand myCommand				= new SqlCommand("dbo.adn_GetAllPortalTabs", myConnection);
			myCommand.CommandType				= CommandType.StoredProcedure;
			
            AddPortalIDParameters(myCommand,portalID);

            SqlParameter parameterPortalLanguage= new SqlParameter(StStr.ATPortalLanguage, SqlDbType.NVarChar, 12);
            parameterPortalLanguage.Value		= base.CurrentUICultureName;
            myCommand.Parameters.Add(parameterPortalLanguage);

            SqlDataAdapter da	                =   new SqlDataAdapter(myCommand);
			
            DataSet ds	                        =   new DataSet("Pages");
            // Read the resultset
			try
			{
				myConnection.Open();
				da.Fill(ds);
				DataTable dt_tbl	            =	ds.Tables[0];
				if ( DBTraceSwitch.Sw.TraceVerbose )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]PagesDB GetTbAllPortalPages portalID: {0}, Cout: {1}", portalID, dt_tbl.Rows.Count),DBTraceSwitch.Sw.Info);
				// insert first record
				try
				{
					DataRow nrw			        =	dt_tbl.NewRow();
					nrw[StStr.DBPageID]	        =	0;
					nrw[StStr.DBPageName]	    =	"Root";
					nrw[StStr.DBPageSkin]	    =	string.Empty;
					nrw[StStr.DBPageOrder]	    =	0;
					nrw[StStr.DBPageNbModules]	=	0;
					dt_tbl.Rows.Add(nrw);
				}
				catch (Exception ex)
				{
					if ( DBTraceSwitch.Sw.TraceError )
                        Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]PagesDB GetTbAllPortalPages Adding root portalID: {0}, ex: {1}", portalID, ex),DBTraceSwitch.Sw.Info);
				}
				// replace nulls by zeros
				foreach (DataRow rw in dt_tbl.Rows)
				{
					if (Convert.IsDBNull(rw["ParentTabID"]) || rw["ParentTabID"] == null)
						rw["ParentTabID"] = 0;
				}
			}
			catch (Exception exp)
			{
				if ( DBTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]PagesDB GetTbAllPortalPages portalID: {0}, ex: {1}", portalID, exp),DBTraceSwitch.Sw.Info);
				throw;
			}
            finally
            {
                if (myConnection.State == System.Data.ConnectionState.Open)
                    myConnection.Close();
            }

            return ds;
		}

		/// <summary>
		/// Returns a table that contains record of pages ordered according their hierarchy
		/// </summary>
        public DataTable GetPagesFlatTable(int portalID)
        {
            string strSQL		= "dbo.adn_GetPageTree " + portalID.ToString();
            SqlDataAdapter da	= new SqlDataAdapter(strSQL, myConnection);

            DataTable dt_tbl	= new DataTable("Pages");
            try
            {
                da.Fill(dt_tbl);
            }
            catch (Exception exp)
            {
                if ( DBTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]PagesDB GetPagesFlatTable portalID: {0}, ex: {1}", portalID, exp),DBTraceSwitch.Sw.Info);
                throw;
            }
            finally
            {
                if (myConnection.State == System.Data.ConnectionState.Open)
                    myConnection.Close();
            }

            return dt_tbl;
        }

		/// <summary>
		/// Get a page by its name and ParentID
		/// </summary>
        public int GetPageByParentID(int portalID, int parentPageID,string pageName)
        {
            SqlCommand myCommand				=   new SqlCommand("dbo.adn_GetPageByParentID", myConnection);
			myCommand.CommandType				=   CommandType.StoredProcedure;
			
            AddPortalIDParameters(myCommand,portalID);

            SqlParameter parameterParentPageID	=   new SqlParameter("@ParentTabID", SqlDbType.Int, 4);
            parameterParentPageID.Value			=   parentPageID;
            myCommand.Parameters.Add(parameterParentPageID);

            SqlParameter parameterTabName		=   new SqlParameter("@TabName", SqlDbType.NVarChar, 50);
            parameterTabName.Value			    =   pageName;
            myCommand.Parameters.Add(parameterTabName);
						
			int parentID			            = 0;
            try
            {
                myConnection.Open();
                parentID			            = (int)myCommand.ExecuteScalar();
            }
            catch (Exception exp)
            {
                if ( DBTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]PagesDB GetPageByParentID portalID: {0}, pageName: {1}, ex  {2}", portalID, pageName, exp),DBTraceSwitch.Sw.Info);
                throw;
            }
            finally
            {
                if (myConnection.State == System.Data.ConnectionState.Open)
                    myConnection.Close();
            }
            return parentID;
        }

        /// <summary>
		/// Get a page's parentID
		/// </summary>
        public int GetPageParentID(int portalID, int pageID)
        {
            SqlCommand myCommand				= new SqlCommand("dbo.adn_GetPagesParentTabID", myConnection);
			myCommand.CommandType				= CommandType.StoredProcedure;
			
            AddPortalIDParameters(myCommand,portalID);

            AddPageIDParameters(myCommand,pageID);
			
			int parentID			            = 0;
            try
            {
                myConnection.Open();
                parentID			            = (int)myCommand.ExecuteScalar();
            }
            catch (Exception exp)
            {
                if ( DBTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]PagesDB GetPageParentID portalID: {0},pageID: {1}, ex : {2}", portalID, pageID, exp),DBTraceSwitch.Sw.Info);
                throw;
            }
            finally
            {
                if (myConnection.State == System.Data.ConnectionState.Open)
                    myConnection.Close();
            }
            return parentID;
        }

		/// <summary>
		/// Get a page's tab order
		/// </summary>
        public int GetPageTabOrder(int portalID, int pageID)
        {
			SqlCommand myCommand				= new SqlCommand("dbo.adn_GetPageTabOrder", myConnection);
			myCommand.CommandType				= CommandType.StoredProcedure;

            AddPortalIDParameters(myCommand,portalID);

            AddPageIDParameters(myCommand,pageID);

            int ret					            = 99990;
            try
            {
                myConnection.Open();
                ret					            = (int)myCommand.ExecuteScalar();
            }
            catch (Exception exp)
            {
                if ( DBTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]PagesDB GetPageTabOrder portalID: {0},pageID: {1}, ex: {2}", portalID, pageID, exp),DBTraceSwitch.Sw.Info);
                throw;
            }
            finally
            {
                if (myConnection.State == System.Data.ConnectionState.Open)
                    myConnection.Close();
            }
            return ret;
        }

		/// <summary>
		/// pages ordered according their hierarchy
		/// </summary>
        public SqlDataReader GetPagesFlat(int portalID)
        {
            SqlCommand myCommand                =   new SqlCommand("dbo.adn_GetTabsFlat", myConnection);
            myCommand.CommandType				=   CommandType.StoredProcedure;
            
            AddPortalIDParameters(myCommand,portalID);

            SqlDataReader result				=   null;
            try
            {
                myConnection.Open();
                result                          =   myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch (Exception exp)
            {
                if ( DBTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]PagesDB GetPagesFlat portalID: {0}, ex: {1}", portalID, exp),DBTraceSwitch.Sw.Info);
                if (result != null)
                    result.Close();
                if (myConnection.State == System.Data.ConnectionState.Open)
                    myConnection.Close();
                throw;
            }
            return result;
        }

		/// <summary>
		/// All pages contained in the parent page sent
		/// </summary>
        public SqlDataReader GetPagesInPage(int portalID, int pageID)
        {
            SqlCommand myCommand            =   new SqlCommand("dbo.adn_GetTabsinTab", myConnection);
            myCommand.CommandType			=   CommandType.StoredProcedure;
            
            AddPortalIDParameters(myCommand,portalID);

            AddPageIDParameters(myCommand,pageID);

            SqlDataReader result			=   null;
            try
            {
                myConnection.Open();
                result = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch (Exception exp)
            {
                if ( DBTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]PagesDB GetPagesInPage portalID : {0}, ex : {1}", portalID, exp),DBTraceSwitch.Sw.Info);
                if (result != null)
                    result.Close();
                if (myConnection.State == System.Data.ConnectionState.Open)
                    myConnection.Close();
                throw;
            }
            return result;
        }

		/// <summary>
		///     
		/// </summary>
		public SqlDataReader GetPagesParent(int portalID, int pageID) 
		{
			SqlCommand myCommand				= new SqlCommand("dbo.adn_GetTabsParent", myConnection);
			myCommand.CommandType				= CommandType.StoredProcedure;

            AddPortalIDParameters(myCommand,portalID);

			AddPageIDParameters(myCommand,pageID);
            
            SqlDataReader result				= null;
            try
            {
                myConnection.Open();
                result							= myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch (Exception exp)
            {
                if ( DBTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]PagesDB GetPagesParent portalID: {0}, ex: {1}", portalID, exp),DBTraceSwitch.Sw.Info);
                if (result != null)
                    result.Close();
                if (myConnection.State == System.Data.ConnectionState.Open)
                    myConnection.Close();
                throw;
            }
            return result;
        }

        /// <summary>
        /// Read Current Page subpages, automatically switches between admin or not sitemap
        /// </summary>
        public SqlDataReader GetPageSettingsPages(int portalID,int PageID)
        {
            // we switch site map if necessary
            SqlCommand myCommand					=   
                new SqlCommand(( Config.IsAdminPortal ) ? "dbo.adn_GetTabSettings": "dbo.adn_GetPageSettings", myConnection);
            myCommand.CommandType					=   CommandType.StoredProcedure;
            
            AddPortalIDParameters(myCommand,portalID);

			AddPageIDParameters(myCommand,PageID);

            SqlParameter parameterPortalLanguage	=   new SqlParameter(StStr.ATPortalLanguage, SqlDbType.NVarChar, 12);
            parameterPortalLanguage.Value			=   base.CurrentUICultureName;
            myCommand.Parameters.Add(parameterPortalLanguage);
            
            SqlDataReader result                    =   null;
            try
            {
                myConnection.Open();
                result                              =   myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch (Exception exp)
            {
                if ( DBTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]PagesDB GetPageSettingsPages PageID: {0}, ex: {1}", PageID, exp),DBTraceSwitch.Sw.Info);
                if (result != null)
                    result.Close();
                if (myConnection.State == System.Data.ConnectionState.Open)
                    myConnection.Close();
                throw;
            }
            return result;
        }

		public SqlDataReader GetUrlName(int pageID) 
		{
			SqlCommand myCommand				= new SqlCommand("dbo.adn_GetUrlName", myConnection);
			myCommand.CommandType				= CommandType.StoredProcedure;

			AddPageIDParameters(myCommand,pageID);
            
            SqlDataReader result				= null;
            try
            {
                myConnection.Open();
                result							= myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch (Exception exp)
            {
                if ( DBTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]PagesDB GetUrlName PageId: {0}, ex: {1}", pageID, exp),DBTraceSwitch.Sw.Info);
                if (result != null)
                    result.Close();
                if (myConnection.State == System.Data.ConnectionState.Open)
                    myConnection.Close();
                throw;
            }
            return result;
        }

        public void UpdateUrlName(int pageID, string urlName)
        {
            SqlConnection theConnection             =   myConnection;
            if ( theConnection.State == ConnectionState.Open )
                theConnection		                =   new SqlConnection(Config.ConnectionString);
            using (SqlCommand myCommand = new SqlCommand("dbo.adn_UpdateUrlName", theConnection))
            {
                myCommand.CommandType				=   CommandType.StoredProcedure;
                
                AddPageIDParameters(myCommand,pageID);
                
                SqlParameter parameterurlName		=   new SqlParameter(StStr.ATUrlName, SqlDbType.NVarChar, 50);
                parameterurlName.Value			    =   urlName;
                myCommand.Parameters.Add(parameterurlName);

                try
                {
                    theConnection.Open();
                    myCommand.ExecuteNonQuery();
                }
                catch (Exception exp)
                {
                    if ( DBTraceSwitch.Sw.TraceError )
                        Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]PagesDB UpdateUrlName pageID: {0}, ex: {1}", pageID, exp),DBTraceSwitch.Sw.Info);
                    throw;
                }
                finally
                {
                    if (theConnection.State == ConnectionState.Open)
                        theConnection.Close();
                }
            }
        }

        public void UpdateUrlAndLayout(int pageID, string urlName,int tabLayout)
        {
            SqlConnection theConnection             =   myConnection;
            if ( theConnection.State == ConnectionState.Open )
                theConnection		                =   new SqlConnection(Config.ConnectionString);
            using (SqlCommand myCommand = new SqlCommand("dbo.adn_UpdateUrlAndLayout", theConnection))
            {
                myCommand.CommandType				=   CommandType.StoredProcedure;
                
                AddPageIDParameters(myCommand,pageID);
                
                SqlParameter parameterurlName		=   new SqlParameter(StStr.ATUrlName, SqlDbType.NVarChar, 50);
                parameterurlName.Value			    =   urlName;
                myCommand.Parameters.Add(parameterurlName);

                SqlParameter parameterTabLaout		=   new SqlParameter(StStr.ATTabLayout, SqlDbType.Int);
                parameterTabLaout.Value			    =   tabLayout;
                myCommand.Parameters.Add(parameterTabLaout);

                try
                {
                    theConnection.Open();
                    myCommand.ExecuteNonQuery();
                }
                catch (Exception exp)
                {
                    if ( DBTraceSwitch.Sw.TraceError )
                        Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]PagesDB UpdateUrlAndLayout pageID: {0}, ex: {1}", pageID, exp),DBTraceSwitch.Sw.Info);
                    throw;
                }
                finally
                {
                    if (theConnection.State == ConnectionState.Open)
                        theConnection.Close();
                }
            }
        }

		public SqlDataReader GetAllUrlNameByPortal(int portalID) 
		{
			SqlCommand myCommand				=   new SqlCommand("dbo.adn_GetAllUrlNameByPortal", myConnection);
			myCommand.CommandType				=   CommandType.StoredProcedure;

            AddPortalIDParameters(myCommand,portalID);
            
            SqlDataReader result				=   null;
            try
            {
                myConnection.Open();
                result							=   myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch (Exception exp)
            {
                if ( DBTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]PagesDB GetAllUrlNameByPortal portalID: {0}, ex: {1}", portalID, exp),DBTraceSwitch.Sw.Info);
                if (result != null)
                    result.Close();
                if (myConnection.State == System.Data.ConnectionState.Open)
                    myConnection.Close();
                throw;
            }
            return result;
        }

		public SqlDataReader GetAllUrlName(int portalID) 
		{
			SqlCommand myCommand				=   new SqlCommand("dbo.adn_GetAllUrlName", myConnection);
			myCommand.CommandType				=   CommandType.StoredProcedure;

            SqlDataReader result				=   null;
            try
            {
                myConnection.Open();
                result							=   myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch (Exception exp)
            {
                if ( DBTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]PagesDB GetAllUrlName portalID: {0}, ex: {1}", portalID, exp),DBTraceSwitch.Sw.Info);
                if (result != null)
                    result.Close();
                if (myConnection.State == System.Data.ConnectionState.Open)
                    myConnection.Close();
                throw;
            }
            return result;
        }

		public SqlDataReader GetAllLoginPages(int portalID) 
		{
			SqlCommand myCommand				=   new SqlCommand("dbo.adn_GetAllLoginPages", myConnection);
			myCommand.CommandType				=   CommandType.StoredProcedure;

            AddPortalIDParameters(myCommand,portalID);
            
            SqlDataReader result				=   null;
            try
            {
                myConnection.Open();
                result							=   myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch (Exception exp)
            {
                if ( DBTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]PagesDB GetAllLoginPages portalID: {0}, ex: {1}", portalID, exp),DBTraceSwitch.Sw.Info);
                if (result != null)
                    result.Close();
                if (myConnection.State == System.Data.ConnectionState.Open)
                    myConnection.Close();
                throw;
            }
            return result;
        }

		public DataTable GetAllLoginPagesTb(int portalID) 
		{

			SqlConnection theCnx				=   newConnection;
            SqlCommand myCommand				=   new SqlCommand("dbo.adn_GetAllLoginPages", theCnx);
			myCommand.CommandType				=   CommandType.StoredProcedure;
            SqlDataAdapter da	                =   new SqlDataAdapter(myCommand);
            
            AddPortalIDParameters(myCommand,portalID);
            
            DataTable tbl	                    =   new DataTable("LoginPages");
            try
            {
                da.Fill(tbl);
            }
            catch (Exception exp)
            {
                if ( DBTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]PagesDB GetAllLoginPages portalID: {0}, ex: {1}", portalID, exp),DBTraceSwitch.Sw.Info);
                throw;
            }
            finally
            {
                if (theCnx.State == System.Data.ConnectionState.Open)
                    theCnx.Close();
            }

            return tbl;
        }

        public SqlDataReader GetAllPagesByFlag(int portalID,int tabLayout) 
		{
			SqlCommand myCommand				=   new SqlCommand("dbo.adn_GetAllPagesByFlag", myConnection);
			myCommand.CommandType				=   CommandType.StoredProcedure;

            AddPortalIDParameters(myCommand,portalID);
            
            SqlParameter parameterTabLaout		=   new SqlParameter(StStr.ATTabLayout, SqlDbType.Int);
            parameterTabLaout.Value			    =   tabLayout;
            myCommand.Parameters.Add(parameterTabLaout);

            SqlDataReader result				=   null;
            try
            {
                myConnection.Open();
                result							=   myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch (Exception exp)
            {
                if ( DBTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]PagesDB GetAllPagesByFlag portalID: {0}, ex: {1}", portalID, exp),DBTraceSwitch.Sw.Info);
                if (result != null)
                    result.Close();
                if (myConnection.State == System.Data.ConnectionState.Open)
                    myConnection.Close();
                throw;
            }
            return result;
        }

		public DataTable GetAllPagesByFlagTb(int portalID,int tabLayout)  
		{

			SqlConnection theCnx				=   newConnection;
            SqlCommand myCommand				=   new SqlCommand("dbo.adn_GetAllPagesByFlag", theCnx);
			myCommand.CommandType				=   CommandType.StoredProcedure;
            SqlDataAdapter da	                =   new SqlDataAdapter(myCommand);
            
            AddPortalIDParameters(myCommand,portalID);
            
            SqlParameter parameterTabLaout		=   new SqlParameter(StStr.ATTabLayout, SqlDbType.Int);
            parameterTabLaout.Value			    =   tabLayout;
            myCommand.Parameters.Add(parameterTabLaout);

            DataTable tbl	                    =   new DataTable("FlagedPages");
            try
            {
                da.Fill(tbl);
            }
            catch (Exception exp)
            {
                if ( DBTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]PagesDB GetAllPagesByFlagTb portalID: {0}, ex: {1}", portalID, exp),DBTraceSwitch.Sw.Info);
                throw;
            }
            finally
            {
                if (theCnx.State == System.Data.ConnectionState.Open)
                    theCnx.Close();
            }
            return tbl;
        }

        /// <summary>
        /// The PageSettings.GetPageCustomSettings Method returns 
        /// custom Page specific settings from the database. This method is
        /// used by Portals to access misc Page settings.
        /// We must force a new connection because the method is called inside another
        /// </summary>
        public SqlDataReader GetPageCustomSettings(int pageID)
        {
            SqlConnection theCnx				=   newConnection;
            SqlCommand myCommand				=   new SqlCommand("dbo.adn_GetTabCustomSettings", theCnx);
            myCommand.CommandType				=   CommandType.StoredProcedure;
            
			AddPageIDParameters(myCommand,pageID);

			SqlDataReader result				=   null;
			try
            {
                theCnx.Open();
                result							=   myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch (Exception exp)
            {
                if ( DBTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]PagesDB GetPageCustomSettings PageID : {0}, ex : {1}", pageID, exp),DBTraceSwitch.Sw.Info);
                if (result != null)
                    result.Close();
                if (theCnx.State == System.Data.ConnectionState.Open)
                    theCnx.Close();
                throw;
            }
            return result;
        }

        /// <summary>
        /// The GetPageSingleCustomSettings Method returns 
        /// one custom Page specific settings from the database. 
        /// </summary>
        public string GetPageSingleCustomSettings(int pageID,string SettingName)
        {
            SqlConnection theCnx				    =   newConnection;
            SqlCommand myCommand				    =   new SqlCommand("dbo.adn_GetTabSingleCustomSetting", theCnx);
            myCommand.CommandType				    =   CommandType.StoredProcedure;
            
			AddPageIDParameters(myCommand,pageID);

            SqlParameter parameterSettingName		=   new SqlParameter(StStr.ATSettingName, SqlDbType.NVarChar, 50);
            parameterSettingName.Value				=   SettingName;
            myCommand.Parameters.Add(parameterSettingName);
			
            SqlParameter parameterSettingValue		=   new SqlParameter(StStr.ATSettingValue, SqlDbType.NVarChar, 1500);
            parameterSettingValue.Direction         =   ParameterDirection.Output;
            myCommand.Parameters.Add(parameterSettingValue);
            
            string result                           =   string.Empty;
            try
            {
                theCnx.Open();
                myCommand.ExecuteNonQuery();
                if ( parameterSettingValue.Value != DBNull.Value )
                    result                          =   (string)parameterSettingValue.Value;
            }
            catch (Exception exp)
            {
                if ( DBTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]PagesDB GetPageSingleCustomSettings PageID : {0}, SettingName: {1}, ex : {2}", pageID,SettingName,exp),DBTraceSwitch.Sw.Info);
                throw;
            }
            finally
            {
                if (theCnx.State == System.Data.ConnectionState.Open)
                        theCnx.Close();
            }
            return result;
        }

		#endregion Get Pages

        #region SiteMap

        /// <summary>
		/// Delete the SIteMap for this portal.
		/// Returns a SqlDataReader
		/// </summary>
		public void DeleteSiteMap(int portalID)
		{
			SqlCommand myCommand				=   new SqlCommand("dbo.adn_DeleteSiteMap", myConnection);
			myCommand.CommandType				=   CommandType.StoredProcedure;

            AddPortalIDParameters(myCommand,portalID);

            try
            {
                myConnection.Open();
                myCommand.ExecuteNonQuery();
            }
            catch (Exception exp)
            {
                if ( DBTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]PagesDB DeleteSiteMap PortalID : {0}, ex : {1}", portalID, exp),DBTraceSwitch.Sw.Info);
                throw;
            }
            finally
            {
                if (myConnection.State == System.Data.ConnectionState.Open)
                    myConnection.Close();
            }
        }

		/// <summary>
		/// Get all the pages in this Site ordered by asc parentTabID and order.
		/// Returns a SqlDataReader
		/// </summary>
		public SqlDataReader GetAllSitePortalPages(int portalID)
		{
			SqlCommand myCommand				    =   new SqlCommand("dbo.adn_GetAllPortalPages", myConnection);
			myCommand.CommandType				    =   CommandType.StoredProcedure;
			
            AddPortalIDParameters(myCommand,portalID);

            SqlParameter parameterPortalLanguage	=   new SqlParameter(StStr.ATPortalLanguage, SqlDbType.NVarChar, 12);
            parameterPortalLanguage.Value			=   base.CurrentUICultureName;
            myCommand.Parameters.Add(parameterPortalLanguage);

            SqlDataReader result				    =   null;
            try
            {
                myConnection.Open();
                result							    =   myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch (Exception exp)
            {
                if ( DBTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]PagesDB GetAllPortalSitePages portalID: {0}, ex: {1}", portalID, exp),DBTraceSwitch.Sw.Info);
                if (result != null)
                    result.Close();
                if (myConnection.State == System.Data.ConnectionState.Open)
                    myConnection.Close();
                throw;
            }
            return result;
        }


        
        public void AddSiteMapPage(int portalID,int PageID,int ParentPageID,int PageOrder)
        {
			SqlCommand myCommand				=   new SqlCommand("dbo.adn_AddSiteMapPage", myConnection);
			myCommand.CommandType				=   CommandType.StoredProcedure;
			
            AddPortalIDParameters(myCommand,portalID);

            AddSitePageIDParameters(myCommand,PageID);

            SqlParameter parameterParentPageID	=   new SqlParameter("@ParentPageID", SqlDbType.Int, 4);
			parameterParentPageID.Value		    =   ParentPageID;
			myCommand.Parameters.Add(parameterParentPageID);

            SqlParameter parameterPageOrder		=   new SqlParameter("@PageOrder", SqlDbType.Int, 4);
			parameterPageOrder.Value			=   PageOrder;
			myCommand.Parameters.Add(parameterPageOrder);

            try
            {
                myConnection.Open();
                myCommand.ExecuteNonQuery();
            }
            catch (Exception exp)
            {
                if ( DBTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]PagesDB AddSiteMapPage PortalID: {0}, PageID: {1}, ex: {2}", portalID,PageID, exp),DBTraceSwitch.Sw.Info);
                throw;
            }
            finally
            {
                if (myConnection.State == System.Data.ConnectionState.Open)
                    myConnection.Close();
            }
        }
        #endregion SiteMap

        #region Page Settings

        /// <summary>
		/// Update Page Custom Settings
		/// </summary>
        public void UpdatePageSettings(int pageID, string key, string value)
        {
            SqlConnection theCnx				=   new SqlConnection(Config.ConnectionString);
            SqlCommand myCommand				=   new SqlCommand("dbo.adn_UpdateTabCustomSettings", theCnx);
            myCommand.CommandType				=   CommandType.StoredProcedure;
            
			AddPageIDParameters(myCommand,pageID);

            SqlParameter parameterKey			=   new SqlParameter(StStr.ATSettingName, SqlDbType.NVarChar, 50);
            parameterKey.Value					=   key.ToUpper();
            myCommand.Parameters.Add(parameterKey);

            SqlParameter parameterValue			=   new SqlParameter(StStr.ATSettingValue, SqlDbType.NVarChar, 1500);
            parameterValue.Value				=   value;
            myCommand.Parameters.Add(parameterValue);
            
            try
            {
                theCnx.Open();
                myCommand.ExecuteNonQuery();
            }
            catch (Exception exp)
            {
                if ( DBTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]PagesDB UpdatePageSettings pageID: {0}, ex: {1}", pageID, exp),DBTraceSwitch.Sw.Info);
                throw;
            }
            finally
            {
                if (theCnx.State == System.Data.ConnectionState.Open)
                    theCnx.Close();
            }
        }
		#endregion Get Pages
	}
}
